# -*- coding: utf-8 -*-
import calendar
from datetime import *

fmt = '%Y-%m-%d %H:%M:%S'
fmt_pure = '%Y%m%d%H%M%S'
fmt_dateonly = '%Y-%m-%d'
fmt_minute = '%Y-%m-%d %H:%M'
fmt_msec = '%Y-%m-%d %H:%M:%S.%f'

def curr_now_msec():
    return datetime.now().strftime(fmt_msec)

def curr_datetime_now():
    return datetime.now()

# 2014-09-25 17:21:30
def curr_now():
    return datetime.now().strftime(fmt)

# 20140925172101
def curr_now_pure():
    return datetime.now().strftime(fmt_pure)

def curr_now_dateonly():
    return datetime.now().strftime(fmt_dateonly)

def curr_now_fmt(p_fmt):
    return datetime.now().strftime(p_fmt)

def date_format(p_date, fmt=fmt):
    return datetime.strptime(p_date, fmt)

def reformat(str_date, fmt_in, fmt_out, _default="19990101000000"):
    try:
        return datetime.strptime(str_date, fmt_in).strftime(fmt_out)
    except Exception,e:
        return reformat(_default, fmt_in, fmt_out)

def yesterday(fmt):
    tmp = datetime.now() + timedelta(days=-1)
    return tmp.strftime(fmt)

def tomorrow(fmt):
    tmp = datetime.now() + timedelta(days=1)
    return tmp.strftime(fmt)

# 加月
def add_months(dt, months):
    month = dt.month - 1 + months
    year = dt.year + month / 12
    month = month % 12 + 1
    day = min(dt.day, calendar.monthrange(year, month)[1])
    return dt.replace(year=year, month=month, day=day)

def format_date(y, m, d):
    # yyyy-mm-dd
    tmp = date(int(y), int(m), int(d)).isoformat()
    return tmp

#只有日期的加月dt:datetime.now()
def add_months_dateonly(dt, months):
    month = dt.month - 1 + months
    year = dt.year + month / 12
    month = month % 12 + 1
    day = min(dt.day, calendar.monthrange(year,month)[1])
    return dt.replace(year=year, month=month, day=day).strftime(fmt_dateonly)

def str_to_date(str_date, fmt='%Y-%m-%d'):
    import time
    d = time.strptime(str_date, fmt)
    return date(d.tm_year, d.tm_mon, d.tm_mday)

def default_birth():
    """
    宝宝的默认生日
    :return:
    """
    return add_months(date.today(), -36)

if __name__ == "__main__":
    pass
